package xin.printk.android.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import xin.printk.android.BasicFragment;
import xin.printk.android.R;
import xin.printk.android.data.event.LoginEvent;
import xin.printk.android.module.register.RegisterActivity;
import xin.printk.android.utils.LogUtils;

public class LoginFragment extends BasicFragment implements LoginContract.View {

    private Button mLoginButton;
    private EditText mAccountText;
    private EditText mPasswordText;
    private TextView mRegisterText;
    private TextView mForgetPasswordText;
    private LoginContract.Presenter mPresenter;

    @Override
    protected int bindLayoutResId() {
        return R.layout.fragment_login;
    }

    @Override
    protected void onInit() {
        mRegisterText = findView(R.id.tv_register);
        mForgetPasswordText = findView(R.id.tv_forget_password);
        mAccountText = findView(R.id.et_login_account);
        mPasswordText = findView(R.id.et_login_password);
        mLoginButton = findView(R.id.bt_login);
    }

    @Override
    protected void onExecute(Bundle savedInstanceState) {
        mRegisterText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), RegisterActivity.class));
            }
        });
        mForgetPasswordText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(mForgetPasswordText, getString(R.string.forget_password_tips), Snackbar.LENGTH_LONG).show();
            }
        });
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.login();
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new LoginPresenter(this);
        if (!EventBus.getDefault().isRegistered(this)) {
            LogUtils.d("LoginFragment register EventBus");
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            LogUtils.d("LoginFragment unregister EventBus");
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public String getAccount() {
        return mAccountText.getText().toString();
    }

    @Override
    public String getPassword() {
        return mPasswordText.getText().toString();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvent event) {
        LogUtils.d("login receive event");
        if (event.isOnline()) {
            getActivity().finish();
        }
    }

    @Override
    public void loginSuccess() {
        EventBus.getDefault().post(new LoginEvent(true));
    }
}
